

<?php
/*
 * Script file for Sand Dollar Media
 * ===============================================
 * Date: March 17th, 2012
 * Created By: Integration Team
 * ===============================================
 * Criteria:
 * 1. Only IM Skus 
 * 2. Only skus that have QTY > 0
 * 3. MAP field from IM file needs to go in minimal_price column if MAP is 0, leave blank 
 * 4. Suffix sku field with 'im' or 'td' - e.g. 1234td
 * 5. use custom category tree in custom_category table for categories column 
 * 6. Manufacturer name replacements - refer to cleanup_manufacturers()
 * 7. URL key should only contain alpha-numeric with only - replacing spaces - refer to URL_key()
 *  
 */
set_time_limit(3000);
require_once 'config.php';

$file = new FileWriter(Output_File_Name,Output_File_Directory,'a',TRUE);
$logfile = new FileWriter('log.txt', Log_File_Directory, 'a', FALSE);
$content = "";
$records = 0;


    
//Product File
$start_time = time();

$product_header_array = array('sku',
                                    'categories',
                                    'meta_title',
                                    'description',
                                    '_store',
                                    '_attribute_set',
                                    '_type',
                                    '_product_websites',
                                    'color',
                                    'cost',
                                    'special_price',
                                    'price',
                                    'country_of_manufacture',
                                    'has_options',
                                    'enable_googlecheckout',
                                    'custom_design',
                                    'custom_design_from',
                                    'custom_design_to',
                                    'custom_layout_update',
                                    'gallery',
                                    'gift_message_available',
                                    'image',
                                    'image_label',
                                    'media_gallery',
                                    'meta_description',
                                    'meta_keyword',
                                    'minimal_price',
                                    'msrp',
                                    'msrp_display_actual_price_type',
                                    'msrp_enabled',
                                    'options_container',
                                    'news_from_date',
                                    'news_to_date',
                                    'page_layout',
                                    'required_options',
                                    'short_description',
                                    'small_image',
                                    'small_image_label',
                                    'special_from_date',
                                    'special_to_date',
                                    'status',
                                    'tax_class_id',
                                    'thumbnail',
                                    'thumbnail_label',
                                    'url_key',
                                    'visibility',
                                    'min_qty',
                                    'use_config_min_qty',
                                    'is_qty_decimal',
                                    'backorders',
                                    'use_config_backorders',
                                    'min_sale_qty',
                                    'use_config_min_sale_qty',
                                    'max_sale_qty',
                                    'use_config_max_sale_qty',
                                    'is_in_stock',
                                    'notify_stock_qty',
                                    'use_config_notify_stock_qty',
                                    'manage_stock',
                                    'use_config_manage_stock',
                                    'stock_status_changed_auto',
                                    'use_config_qty_increments',
                                    'qty_increments',
                                    'use_config_enable_qty_inc',
                                    'enable_qty_increments',
                                    '_links_related_sku',
                                    '_links_related_position',
                                    '_links_crosssell_sku',
                                    '_links_crosssell_position',
                                    '_links_upsell_sku',
                                    '_links_upsell_position',
                                    '_associated_sku',
                                    '_associated_default_qty',
                                    '_associated_position',
                                    '_tier_price_website',
                                    '_tier_price_customer_group',
                                    '_tier_price_qty',
                                    '_tier_price_price',
                                    '_media_lable',
                                    '_media_position',
                                    '_media_is_disabled',
                                    'qty',
                                    'name',
                                    'manufacturer',
                                    'weight',
                                    '_media_image',
                                    'upc');     

$product_header_string = '"';
$product_header_string .= implode('"'.Delimiter.'"', $product_header_array);
$product_header_string .= "\"".Line_Terminator;



$file->write($product_header_string);

$categorytemplate = new categoryClass();

$result = query("Select bp.productid as productId,
                p.categoryid as categoryId,
                bp.sku as sku,
                bp.ManufacturerName as manufacturerName,
                bp.MfgPartNo as mfgPartNo,
                bp.Cost as cost,
                bp.RetailPrice as retailPrice,
                bp.Qty as qty,
                bp.UPC as upc,
                bp.ShipWeight as shipWeight,
                bp.ShipLength as shipLength,
                bp.ShipWidth as shipWidth,
                bp.ShipHeight as shipHeight,
                mc.customcategorytree as categoryTree,
                pk.keywords as keywords,
                mn.name as Manufacturer,
                if(bp.MAP=0,'',bp.MAP) as MAP
                from ".Custom_DB_name.".flatBestPrice bp
                join ".Spex_DB_name.".product p on bp.productid = p.productid
                join ".Custom_DB_name.".mapped_category mc on p.categoryid = mc.categoryid
                join ".Spex_DB_name.".productkeywords pk on p.productid = pk.productid and pk.localeid = 1
                join ".Spex_DB_name.".manufacturer mn on p.manufacturerid = mn.manufacturerid 
                
                ;
                
                ");

while($row = mysql_fetch_assoc($result)){
    
    $price = 0;
    
        if($row['retailPrice']==0)
        {
            //If the retail price is 0 then add 50% to the cost and this should be used as the price
            $price = $row['cost'] * 1.5;
        }
        else
        {
            $price = $row['retailPrice'];
        }
        if($price ==0){
            $logfile->write(date("Y.m.d g:i a").$row['sku']." has 0 for both price and cost");
        }
     
    $cda = $categorytemplate->getCategoryDisplayAttributes($row['categoryId']);    
    $product = new product($row['productId'], $cda, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE);
    if($product->get_description(2)==''){
        $logfile->write(date("Y.m.d g:i a").$row['productId']." is missing Description Type 2".Line_Terminator);
        if($product->get_description(1)==''){
            $logfile->write(date("Y.m.d g:i a").$row['productId']." is missing Both Description Type 2 and 1".Line_Terminator);
            $description = "";
        }
        else{
            $description = $product->get_description(1);
        }
        
    }
    else{
        $description = $product->get_description(2);
    }
    
    
    if($description<>''){
    $content.= '"'.$row['sku'].'","'.
                $row['categoryTree'].'","'.
                cleanup($product->get_description(1)).'","'.
                cleanup($product->get_product_overview()).'","'.
                '","'.
                'Default'.'","'.
                'Simple'.'","'.
                'base'.'","'.
                cleanup($product->get_attribute(3411122)).'",'.
                $row['cost'].','.
                ','.round($price,2).','.
                '"",'.
                '0,'.
                '1,'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '"","'.
                $product->get_largest_image().'","'.
                cleanup($description).'",'.
                '"",'.
                '"","'.
                cleanup($row['keywords']).'",'.
                $row['MAP'].','.
                $row['retailPrice'].','.
                '"Use Config",'.
                '"Use Config",'.
                '"Block After Info Column",'.
                '"",'.
                '"",'.
                '"",'.
                '0,"'.
                cleanup($product->get_description(1)).'","'.
                $product->get_largest_image().'","'.
                cleanup($description).'",'.
                '"",'.
                '"",'.
                '1,'.
                '0,"'.
                $product->get_largest_image().'",'.
                '"","'.
                URL_key($description).'",'.
                '4,'.
                '0,'.
                '1,'.
                '0,'.
                '0,'.
                '1,'.
                '1,'.
                '1,'.
                '0,'.
                '1,'.
                '1,'.
                '"",'.
                '1,'.
                '0,'.
                '1,'.
                '0,'.
                '1,'.
                '0,'.
                '1,'.
                '0,'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '"",'.
                '1,'.
                '0,'.
                $row['qty'].',"'.
                cleanup($product->get_description(2)).'","'.
                cleanup_manufacturer(cleanup_manufacturer($row['Manufacturer'])).'",'.
                $row['shipWeight'].','.
                '"","'.
                $row['upc'].'"'.Line_Terminator;
        $records++;
    }
    
unset($product); 

    if($records==Record_Buffer)
    {
        $file->write($content);
        $content = "";                
        $records = 0;           
    }

}
if ($content <> "")
{
    //Dump remaining records that are left over and didn't reach the record buffer value 
    $file->write($content);                
    $file->close_handle();
}


$logfile->write(time() - $start_time." seconds".Line_Terminator);

?>

</html>